'use strict';

const Controller = require('egg').Controller;

class UsersController extends Controller {
    //登录接口
  async logins() {
    const {name,password}=this.ctx.request.body
    const loginData=await this.app.mysql.query(`select * from user where name=? and password=?`,[name,password])
    if(loginData.length){
        this.ctx.body={
            code:1,
            msg:"登录成功",
            loginData
        }
    }else{
        this.ctx.body={
            code:0,
            msg:"登录失败，请重新登录"
        }
    }

  }
  //注册接口
  async regists(){
      const {name,password}=this.ctx.request.body
      const queryData=await this.app.mysql.query(`select * from user where name=?`,[name])
      if(queryData.length){
            this.ctx.body={
                code:1,
                msg:"查找成功，请重新注册"
            }
      }else{
          const registData=await this.app.mysql.query(`insert into user (name,password) value (?,?)`,[name,password])
          if(registData.affectedRows){
              this.ctx.body={
                  code:1,
                  msg:"注册成功",
                  registData
              }
          }else{
              this.ctx.body={
                  code:0,
                  msg:"注册失败，请重新注册"
              }
          }
      }
  }
  //查看全表数据
       async allDatas() {
        const allDataList = await this.app.mysql.select("goods")
        if (allDataList) {
            this.ctx.body = {
                code: 1,
                msg: "查找成功",
                allDataList
            }
        } else {
            this.ctx.body = {
                code: 0,
                msg: "查询失败"
            }
        }

    }
  //添加接口（增）
  async addData(){
      const {dataTime,avatar,goodsName,price,sales,stock,sort}=this.ctx.request.body
      const addinsertData=await this.app.mysql.insert("goods",{
          dataTime,avatar,goodsName,price,sales,stock,sort
      })
      if(addinsertData.affectedRows){
          this.ctx.body={
              code:1,
              msg:"数据添加成功",
              addinsertData
          }
      }else{
        this.ctx.body={
            code:0,
            msg:"数据添加失败,请重新添加",
        }
    }
  }
  //删除接口(删)
  async resData(){
  const { id }=this.ctx.request.body
  const deleteData=await this.app.mysql.delete("goods",{
      id
  })
   if(deleteData.affectedRows){
       this.ctx.body={
           code:1,
           msg:"删除成功",
           deleteData
       }
   }else{
       this.ctx.body={
           code:0,
           msg:"删除失败,请重新删除"
       }
   }
  }
  //修改接口（改）
  async alertData(){
    const {dataTime,avatar,goodsName,price,sales,stock,sort,id}=this.ctx.request.body
    const alertDatas=await this.app.mysql.query(`update goods set dataTime=? ,avatar=?,goodsName=?,price=?,sales=?,stock=?,sort=? where id=?`,[dataTime,avatar,goodsName,price,sales,stock,sort,id])
    if(alertDatas.affectedRows){
           this.ctx.body={
               code:1,
               msg:"修改成功",
               alertDatas
           }
    }else{
        this.ctx.body={
            code:0,
            msg:"修改失败",
        }
    }
  }
  //查看接口 （查）
  async checkData(){
      const {id}=this.ctx.request.body
      const checkDatas=await this.app.mysql.get("goods",{id})
      if(checkDatas){
           this.ctx.body={
               code:1,
               msg:"查看成功",
               checkDatas
           }
      }else{
          this.ctx.body={
              code:0,
              msg:"查看失败,请重新查找"
          }
      }
  }

}

module.exports = UsersController;